﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.PWDB;

namespace BPM.Foundation.PWService
{
    public partial class PowerService : BPM.Foundation.PWService.IPowerService
    {
        //[4.1	得到人员扩展信息]
        public List<PW_UserExpansionInfo> getUserExpansionInfo(string pid,string userNo)
        {
            //[1]
            if (string.IsNullOrEmpty(userNo))
            {
                return null;
            }
            //-

            //[2]
            int n = PWDB.PW_User.Count(p => p.userNo == userNo);
            if (n != 1)
            {
                return null;
            }
            //-

            //[3]
            var v = PWDB.PW_UserExpansionInfo.Where(p => p.businessKey == userNo).ToList();

            if (v == null)
            {
                return null;
            }
            else
            {
                return v;
            }
            //-
        }
        //-

        //[4.2	更新人员扩展信息]
        public int? updateUserExpansionInfo(string pid,List<PW_UserExpansionInfo> userExpansionInfo)
        {
           

            //[1]
            if (userExpansionInfo == null)
            {
                return 1;
            }

            //[2]
            if (userExpansionInfo.Count == 0)
            {
                return 2;
            }

            string userNo = userExpansionInfo[0].businessKey;

            //[3]
           

            int uen = userExpansionInfo.Count(p => p.businessKey == userNo);

            if (uen != userExpansionInfo.Count)
            {
                return 3;
            }
            //-

            //[4]

            int un = PWDB.PW_User.Count(p => p.userNo == userNo);
            if (un != 1)
            {
                return 4;
            }
            //-

            //[5]

            var ls = PWDB.PW_UserExpansionInfo.Where(p => p.businessKey == userNo);
            foreach (var tp in ls)
            {
                PWDB.PW_UserExpansionInfo.DeleteOnSubmit(tp);
            }

            //[6]

            foreach (var tp in userExpansionInfo)
            {
                tp.ID = Guid.NewGuid();

                if (string.IsNullOrEmpty(tp.businessKey)) { continue; }
                if (string.IsNullOrEmpty(tp.dataValidation)) { tp.dataValidation=string.Empty; }
                if (string.IsNullOrEmpty(tp.groupA)) { tp.dataValidation = string.Empty; }

                if (string.IsNullOrEmpty(tp.groupB)) { tp.dataValidation= string.Empty; }
                if (string.IsNullOrEmpty(tp.itemName)) { tp.itemName = string.Empty; }
                if (string.IsNullOrEmpty(tp.itemType)) { tp.itemType = "String"; }
                if (string.IsNullOrEmpty(tp.itemValue)) { tp.itemValue = ""; }
                if (string.IsNullOrEmpty(tp.showType)) { tp.showType = "TextBox"; }
                if (tp.isRequired == null) { tp.isRequired = 0; }

                PWDB.PW_UserExpansionInfo.InsertOnSubmit(tp);
            }

            return submit();

        }
        //-

        //[4.3	得到部门扩展信息]
        public List<PW_DepartmentExpansionInfo> getDepartmentExpansionInfo(string pid,string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            int n = PWDB.PW_Department.Count(p => p.departmentNo == departmentNo);
            if (n != 1)
            {
                return null;
            }
            //-

            //[3]
            var v = PWDB.PW_DepartmentExpansionInfo.Where(p => p.businessKey == departmentNo).ToList();

            if (v == null)
            {
                return null;
            }
            else
            {
                return v;
            }
            //-
        }
        //-

        //[4.4	更新部门扩展信息]
        public int? updateDepartmentExpansionInfo(string pid,List<PW_DepartmentExpansionInfo> departmentExpansionInfo)
        {

            //[1]
            if (departmentExpansionInfo == null)
            {
                return 1;
            }

     


            //[2]
            if (departmentExpansionInfo.Count == 0)
            {
                return 2;
            }

            string departmentNo = departmentExpansionInfo[0].businessKey;
            //[3]


            int den = departmentExpansionInfo.Count(p => p.businessKey == departmentNo);

            if (den != departmentExpansionInfo.Count)
            {
                return 3;
            }
            //-

            //[4]

            int un = PWDB.PW_Department.Count(p => p.departmentNo == departmentNo);
            if (un != 1)
            {
                return 4;
            }
            //-

            //[5]

            var ls = PWDB.PW_DepartmentExpansionInfo.Where(p => p.businessKey == departmentNo);
            foreach (var tp in ls)
            {
                PWDB.PW_DepartmentExpansionInfo.DeleteOnSubmit(tp);
            }

            //[6]

            foreach (var tp in departmentExpansionInfo)
            {
                tp.ID = Guid.NewGuid();

                if (string.IsNullOrEmpty(tp.businessKey)) { continue; }
                if (string.IsNullOrEmpty(tp.dataValidation)) { tp.dataValidation = string.Empty; }
                if (string.IsNullOrEmpty(tp.groupA)) { tp.dataValidation = string.Empty; }

                if (string.IsNullOrEmpty(tp.groupB)) { tp.dataValidation = string.Empty; }
                if (string.IsNullOrEmpty(tp.itemName)) { tp.itemName = string.Empty; }
                if (string.IsNullOrEmpty(tp.itemType)) { tp.itemType = "String"; }
                if (string.IsNullOrEmpty(tp.itemValue)) { tp.itemValue = ""; }
                if (string.IsNullOrEmpty(tp.showType)) { tp.showType = "TextBox"; }
                if (tp.isRequired == null) { tp.isRequired = 0; }

                PWDB.PW_DepartmentExpansionInfo.InsertOnSubmit(tp);
            }

            return submit();

           
        }
        //-

        //[4.5	得到职能扩展信息]
        public List<PW_DutyExpansionInfo> getDutyExpansionInfo(string pid,string dutyNo)
        {
            //[1]
            if (string.IsNullOrEmpty(dutyNo))
            {
                return null;
            }
            //-

            //[2]
            int n = PWDB.PW_Duty.Count(p => p.dutyNo == dutyNo);
            if (n != 1)
            {
                return null;
            }
            //-

            //[3]
            var v = PWDB.PW_DutyExpansionInfo.Where(p => p.businessKey == dutyNo).ToList();

            if (v == null)
            {
                return null;
            }
            else
            {
                return v;
            }
            //-
        }
        //-

        //[4.6	更新职能扩展信息]
        public int? updateDutyExpansionInfo(string pid,List<PW_DutyExpansionInfo> dutyExpansionInfo)
        {

            //[1]
            if (dutyExpansionInfo == null)
            {
                return 1;
            }




            //[2]
            if (dutyExpansionInfo.Count == 0)
            {
                return 2;
            }

            string dutyNo = dutyExpansionInfo[0].businessKey;
            //[3]


            int den = dutyExpansionInfo.Count(p => p.businessKey == dutyNo);

            if (den != dutyExpansionInfo.Count)
            {
                return 3;
            }
            //-

            //[4]

            int un = PWDB.PW_Duty.Count(p => p.dutyNo == dutyNo);
            if (un != 1)
            {
                return 4;
            }
            //-

            //[5]

            var ls = PWDB.PW_DutyExpansionInfo.Where(p => p.businessKey == dutyNo);
            foreach (var tp in ls)
            {
                PWDB.PW_DutyExpansionInfo.DeleteOnSubmit(tp);
            }

            //[6]

            foreach (var tp in dutyExpansionInfo)
            {
                tp.ID = Guid.NewGuid();

                if (string.IsNullOrEmpty(tp.businessKey)) { continue; }
                if (string.IsNullOrEmpty(tp.dataValidation)) { tp.dataValidation = string.Empty; }
                if (string.IsNullOrEmpty(tp.groupA)) { tp.dataValidation = string.Empty; }

                if (string.IsNullOrEmpty(tp.groupB)) { tp.dataValidation = string.Empty; }
                if (string.IsNullOrEmpty(tp.itemName)) { tp.itemName = string.Empty; }
                if (string.IsNullOrEmpty(tp.itemType)) { tp.itemType = "String"; }
                if (string.IsNullOrEmpty(tp.itemValue)) { tp.itemValue = ""; }
                if (string.IsNullOrEmpty(tp.showType)) { tp.showType = "TextBox"; }
                if (tp.isRequired == null) { tp.isRequired = 0; }

                PWDB.PW_DutyExpansionInfo.InsertOnSubmit(tp);
            }

            return submit();
        }
        //-
    }
}